package service.impl;

import global.Constants;
import global.security.SessionUtils;

import java.sql.Timestamp;
import java.util.List;

import service.TodoCategoryService;
import service.TodoService;
import util.StringUtils;
import dao.TodoCategoryDAO;
import domain.Todo;
import domain.TodoCategory;

/**
 * 任务分类Service实现类.
 * <p>
 * 创建日期：2010-07-01<br>
 * 创建人：Xiyt<br>
 * 修改日期：<br>
 * 修改人：<br>
 * 修改内容：<br>
 * 
 * @author Xiyt
 * @version 1.0
 */
public class TodoCategoryServiceImpl implements TodoCategoryService {
	/**
	 * 账目Service
	 * */
	private TodoService todoService = null;

	/**
	 * 任务分类DAO
	 * */
	private TodoCategoryDAO todoCategoryDao = null;

	/**
	 * 设定任务分类DAO
	 * */
	public void setTodoCategoryDao(TodoCategoryDAO todoCategoryDao) {
		this.todoCategoryDao = todoCategoryDao;
	}
	
	/**
	 * 设定账目Service
	 */
	public void setTodoService(TodoService todoService) {
		this.todoService = todoService;
	}



	/**
	 * 根据条件查询任务分类列表
	 * @param paramTodoCategory 任务分类条件
	 * @return List 任务分类列表
	 */
	public List<TodoCategory> selectByCriteria(TodoCategory paramTodoCategory) {
		// 取得todoCategoryList
		paramTodoCategory = getConditions(paramTodoCategory);
		List<TodoCategory> todoCategoryList = todoCategoryDao.selectByCriteria(paramTodoCategory);
		return todoCategoryList;
	}

	/**
	 * 添加任务分类
	 * @param todoCategory 任务分类
	 */
	public void insert(TodoCategory todoCategory) {
		// 删除区分：未删除
		todoCategory.setDelflag(Constants.DEL_FLAG_1);
		// 系统时间
		Timestamp sysdate = new Timestamp(System.currentTimeMillis());
		todoCategory.setCreateUser(SessionUtils.getCurrentUserId());
		todoCategory.setCreateTime(sysdate);
		todoCategory.setUpdateUser(SessionUtils.getCurrentUserId());
		todoCategory.setUpdateTime(sysdate);
		todoCategoryDao.insert(todoCategory);
	}

	/**
	 * 修改任务分类
	 * @Description: 修改任务分类
	 * @param todoCategory 任务分类
	 */
	public void update(TodoCategory todoCategory) {
		// 系统时间
		Timestamp sysdate = new Timestamp(System.currentTimeMillis());
		todoCategory.setUpdateUser(SessionUtils.getCurrentUserId());
		todoCategory.setUpdateTime(sysdate);
		todoCategoryDao.updateByPrimaryKey(todoCategory);
	}

	/**
	 * 删除任务分类
	 * @param categoryId 任务分类ID
	 */
	public void delete(Integer categoryId) {
		Todo paramTodo = new Todo();
		paramTodo.setCategoryId(categoryId);
		List<Todo> todoList = todoService.selectByCriteria(paramTodo);
		String todoIdStr = "";
		for (Todo todo : todoList) {
			todoIdStr = todoIdStr + todo.getTodoId() + ",";
		}
		if(StringUtils.isNotEmpty(todoIdStr)){
			// 删除账目
			todoService.delete(todoIdStr);
		}
		// 删除任务分类
		todoCategoryDao.deleteByPrimaryKey(categoryId);
	}
	
	/**
	 * 设置公共参数
	 * @param paramTodoCategory 参数对象
	 */
	private TodoCategory getConditions(TodoCategory paramTodoCategory){
		paramTodoCategory.setCreateUser(SessionUtils.getCurrentUserId());// 当前用户
		return paramTodoCategory;
	}
}
